<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师名字" prop="teacherName">
        <el-select v-model="queryParams.teacherName" clearable placeholder="请选择教师名字">
          <el-option
            v-for="teacherList in teacherList"
            :key="teacherList.name"
            :label="teacherList.name"
            :value="teacherList.name"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="月份" prop="assessmentMonth">
        <el-date-picker
          v-model="queryParams.assessmentMonth"
          align="right"
          type="date"
          placeholder="选择日期"

          @keyup.enter.native="handleQuery">
        </el-date-picker>

      </el-form-item>


      <el-form-item label="审核状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="审核状态(0-待自评,1-自评通过,2-审核通过,3-审核不通过)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="4.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="getScoringList"
          v-hasPermi="['performance:scoring:getScoring']"
        >生成本月绩效</el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="completingOneself"
          v-hasPermi="['performance:scoring:completingOneself']"
        >完成自评</el-button>
      </el-col>

       <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="audit"
          v-hasPermi="['performance:scoring:completingOneself']"
        >完成审核</el-button>
      </el-col>




      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['performance:scoring:edit']"
        >修改</el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['performance:scoring:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['performance:scoring:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-row>

    <el-table v-loading="loading" :data="scoringList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="自增长id" align="center" prop="Id" />-->
      <el-table-column label="教师名称" align="center" prop="teacherName" />
<!--      <el-table-column label="教师id" align="center" prop="teacherId" />-->
      <el-table-column label="考核月份" align="center" prop="assessmentMonth" />
      <el-table-column label="考核项目名称" align="center" prop="assessmentName" />
      <el-table-column label="目标值" align="center" prop="target" />

<!--      <el-table-column label="自评分数" align="center" prop="selfScore" />-->
<!--      <el-table-column label="自评备注" align="center" prop="selfRemarks" />-->
<!--      <el-table-column label="审核分数" align="center" prop="assessmentScore" />-->
<!--      <el-table-column label="审核备注" align="center" prop="assessmentRemarks" />-->
      <el-table-column label="自评分数" align="center" prop="selfScore">
        <template #default="{ row }">
          <el-input v-model="row.selfScore" size="mini" />
        </template>
      </el-table-column>

      <el-table-column label="自评备注" align="center" prop="selfRemarks">
        <template #default="{ row }">
          <el-input v-model="row.selfRemarks" size="mini" />
        </template>
      </el-table-column>

      <el-table-column label="审核分数" align="center" prop="assessmentScore">
        <template #default="{ row }">
          <el-input v-model="row.assessmentScore" size="mini" />
        </template>
      </el-table-column>

      <el-table-column label="审核备注" align="center" prop="assessmentRemarks">
        <template #default="{ row }">
          <el-input v-model="row.assessmentRemarks" size="mini" />
        </template>
      </el-table-column>

      <el-table-column label="项目权重" align="center" prop="weight" />
<!--      <el-table-column label="审核人id" align="center" prop="assessmentManId" />-->
      <el-table-column label="审核人" align="center" prop="assessmentManName" />
<!--      <el-table-column label="审核状态(0-待审核,1-审核通过,2-审核不通过)" align="center" prop="state" />-->
      <el-table-column prop="state" label="审核状态" width="120" sortable>
        <template scope="scope">
          <span style="color:deepskyblue" v-if="scope.row.state === 1">自评通过</span>
          <span style="color:green" v-else-if="scope.row.state === 2">审核通过</span>
          <span style="color:red" v-else-if="scope.row.state ===3">审核不通过</span>
          <span style="color:#34bfa3" v-else-if="scope.row.state === 0">待自评</span>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['performance:scoring:edit']"
          >修改</el-button>


          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['performance:scoring:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教师考核打分对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="教师名称" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入教师名称" />
        </el-form-item>
        <el-form-item label="教师id" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师id" />
        </el-form-item>
        <el-form-item label="考核月份" prop="assessmentMonth">
          <el-input v-model="form.assessmentMonth" placeholder="请输入考核月份" />
        </el-form-item>
        <el-form-item label="考核项目名称" prop="assessmentName">
          <el-input v-model="form.assessmentName" placeholder="请输入考核项目名称" />
        </el-form-item>
        <el-form-item label="目标值" prop="target">
          <el-input v-model="form.target" placeholder="请输入目标值" />
        </el-form-item>
        <el-form-item label="自评分数" prop="selfScore">
          <el-input v-model="form.selfScore" placeholder="请输入自评分数" />
        </el-form-item>
        <el-form-item label="自评备注" prop="selfRemarks">
          <el-input v-model="form.selfRemarks" placeholder="请输入自评备注" />
        </el-form-item>
        <el-form-item label="审核分数" prop="assessmentScore">
          <el-input v-model="form.assessmentScore" placeholder="请输入审核分数" />
        </el-form-item>
        <el-form-item label="审核备注" prop="assessmentRemarks">
          <el-input v-model="form.assessmentRemarks" placeholder="请输入审核备注" />
        </el-form-item>
        <el-form-item label="项目权重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入项目权重" />
        </el-form-item>
        <el-form-item label="审核人id" prop="assessmentManId">
          <el-input v-model="form.assessmentManId" placeholder="请输入审核人id" />
        </el-form-item>
        <el-form-item label="审核人" prop="assessmentManName">
          <el-input v-model="form.assessmentManName" placeholder="请输入审核人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listScoring,
  getScoring,
  delScoring,
  addScoring,
  updateScoring,
  ScoringList,
  Oneself, OneOther
} from "@/api/performance/scoring";

import Template from "@/views/performance/template/index.vue";
import {listTeacher} from "@/api/teacher/teacher";

export default {
  name: "Scoring",
  components: {Template},
  data() {
    return {
      row:{
        selfScore: null,
        selfRemarks: null,
        assessmentScore: null,
        assessmentRemarks: null
      },
      // 获取教师列表
      teacherList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师考核打分表格数据
      scoringList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        teacherId: null,
        assessmentMonth: null,
        assessmentName: null,
        target: null,
        selfScore: null,
        selfRemarks: null,
        assessmentScore: null,
        assessmentRemarks: null,
        weight: null,
        assessmentManId: null,
        assessmentManName: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTeacherList();

    this.loading = false;

  },
  methods: {

    completingOneself(){

      Oneself(this.scoringList).then(response => {
        console.log(response);
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    audit(){

      OneOther(this.scoringList).then(response => {
        console.log(response);
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },

    getScoringList(){
      ScoringList().then(response => {
        this.scoringList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getList();
      });
    },
    getTeacherList(){
      this.loading = true;
      listTeacher(this.queryParams).then(response => {
        console.log(response);
        this.teacherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询教师考核打分列表 */
    getList() {
      this.loading = true;
      listScoring(this.queryParams).then(response => {
        this.scoringList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        Id: null,
        teacherName: null,
        teacherId: null,
        assessmentMonth: null,
        assessmentName: null,
        target: null,
        selfScore: null,
        selfRemarks: null,
        assessmentScore: null,
        assessmentRemarks: null,
        weight: null,
        assessmentManId: null,
        assessmentManName: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教师考核打分";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      const id = row.id || this.ids

      getScoring(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教师考核打分";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateScoring(this.scoringList).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScoring(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row.id + "row")
      console.log(this.ids + "this.ids")
      const Ids = row.id || this.ids;

      this.$modal.confirm('是否确认删除教师考核打分编号为"' + Ids + '"的数据项？').then(function() {
        return delScoring(Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('performance/scoring/export', {
        ...this.queryParams
      }, `scoring_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
